﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SocketExtensions.cs" company="Rolosoft Ltd">
//   © Rolosoft Ltd
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

#region License

// Copyright 2013 Rolosoft Ltd
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Rsft.Net.Dns.Extensions
{
    #region Usings

    using System.Diagnostics.Contracts;
    using System.Net.Sockets;

    #endregion

    /// <summary>
    /// The socket extensions.
    /// </summary>
    /// <remarks>
    /// <see cref="http://blogs.msdn.com/b/pfxteam/archive/2011/12/15/10248293.aspx?PageIndex=2#comments"/>
    /// </remarks>
    public static class SocketExtensions
    {
        #region Public Methods and Operators

        /// <summary>
        /// The receive async.
        /// </summary>
        /// <param name="socket">
        /// The socket.
        /// </param>
        /// <param name="awaitable">
        /// The awaitable.
        /// </param>
        /// <returns>
        /// The <see cref="SocketAwaitable"/>.
        /// </returns>
        public static SocketAwaitable ReceiveAsync(this Socket socket, SocketAwaitable awaitable)
        {
            Contract.Requires(awaitable != null);

            awaitable.Reset();
            if (!socket.ReceiveAsync(awaitable.eventArgs))
            {
                awaitable.wasCompleted = true;
            }

            return awaitable;
        }

        /// <summary>
        /// The receive async from.
        /// </summary>
        /// <param name="socket">
        /// The socket.
        /// </param>
        /// <param name="awaitable">
        /// The awaitable.
        /// </param>
        /// <returns>
        /// The <see cref="SocketAwaitable"/>.
        /// </returns>
        public static SocketAwaitable ReceiveFromAsync(this Socket socket, SocketAwaitable awaitable)
        {
            Contract.Requires(awaitable != null);

            awaitable.Reset();
            if (!socket.ReceiveFromAsync(awaitable.eventArgs))
            {
                awaitable.wasCompleted = true;
            }

            return awaitable;
        }

        /// <summary>
        /// The send async.
        /// </summary>
        /// <param name="socket">
        /// The socket.
        /// </param>
        /// <param name="awaitable">
        /// The awaitable.
        /// </param>
        /// <returns>
        /// The <see cref="SocketAwaitable"/>.
        /// </returns>
        public static SocketAwaitable SendAsync(this Socket socket, SocketAwaitable awaitable)
        {
            Contract.Requires(awaitable != null);

            awaitable.Reset();
            if (!socket.SendAsync(awaitable.eventArgs))
            {
                awaitable.wasCompleted = true;
            }

            return awaitable;
        }

        /// <summary>
        /// Sends to async.
        /// </summary>
        /// <param name="socket">The socket.</param>
        /// <param name="awaitable">The awaitable.</param>
        /// <returns>
        /// The <see cref="SocketAwaitable"/>.
        /// </returns>
        public static SocketAwaitable SendToAsync(this Socket socket, SocketAwaitable awaitable)
        {
            Contract.Requires(awaitable != null);

            awaitable.Reset();
            if (!socket.SendToAsync(awaitable.eventArgs))
            {
                awaitable.wasCompleted = true;
            }

            return awaitable;
        }

        #endregion
    }
}